package com.gulimall.coupon.service.impl;

import com.gulimall.coupon.dao.SeckillSkuRelationDao;
import com.gulimall.coupon.entity.SeckillSkuRelationEntity;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.gulimall.coupon.dao.SeckillSessionDao;
import com.gulimall.coupon.entity.SeckillSessionEntity;
import com.gulimall.coupon.service.SeckillSessionService;

import javax.annotation.Resource;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Resource
    private SeckillSessionDao seckillSessionDao;
    @Resource
    private SeckillSkuRelationDao seckillSkuRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取最近三天所有的秒杀活动以及涉及的商品关系数据
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getLasted3DaysList() {
//        List<SeckillSessionEntity> seckillSessionEntityList = seckillSessionDao.selectList(new QueryWrapper<SeckillSessionEntity>()
//                .between("start_time", getNowTime(),  getLasted3DaysTime()));
        //todo 方便调试先使用所有数据
        List<SeckillSessionEntity> seckillSessionEntityList = seckillSessionDao.selectList(null);
        seckillSessionEntityList.forEach(session->{
            List<SeckillSkuRelationEntity> relationList = seckillSkuRelationDao.selectList(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", session.getId()));
            session.setRelationSkus(relationList);
        });
        return seckillSessionEntityList;
    }

    /**
     * 获取当前时间的字符串
     * @return
     */
    private String getNowTime(){
        LocalDate now = LocalDate.now();    // 2021-05-13
        LocalTime min = LocalTime.MIN;      // 一天的最小时间 00:00
        LocalDateTime start = LocalDateTime.of(now, min);

        // 转成对应的字符串格式
        return start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 获取三天后的时间字符串
     * @return
     */
    private String getLasted3DaysTime(){
        LocalDate localDate = LocalDate.now().plusDays(2);  // 2021-05-15
        LocalTime max = LocalTime.MAX;  // 一天的最大时间 23:59:59.999999999
        LocalDateTime end = LocalDateTime.of(localDate, max);

        // 转成字符串格式
        return end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

}